package server

import (
	// "gorm.io/gorm"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

//Student 学生结构体

type Student struct {
	ID   uint   `json:"id" gorm:"primary_key"`
	Name string `json:"name"`
	Age  int    `json:"age"`
}

//studentsHandler 处理学生请求

// func studentsHandler(c *gin.Context) {
// 	db, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
// 	if err != nil {
// 		c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection error"})
// 		return
// 	}
// 	defer db.Close()

// 	var students []Student
// 	db.Table("student").Limit(20).Find(&students)
// 	c.JSON(http.StatusOK, students)

// }

// 初始化数据库连接等操作
func InitDB() (*gorm.DB, error) {
	db, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		return nil, err

	}

	//自动迁移数据库结构
	db.AutoMigrate(&Student{})

	return db, nil

}
